package response;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 演示如何发送一个表示错误的状态码
 */
public class SendErrorCode extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 发送一个错误码
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Sorry, your request has error！");

        // 发送一个其他状态码也是允许的
        //resp.sendError(HttpServletResponse.SC_ACCEPT, "Great");

        //如果调用了sendError之后，再往Writer中写入东西就不管用了
        PrintWriter writer = resp.getWriter();
        writer.write("Sorry!");
        writer.flush();
    }
}
